﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using eschoolBO;

namespace ESCHOOL.admin
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        report rep = new report();
        student stud = new student();
        Classes cl = new Classes();
        subject sub = new subject();
        remark rem = new remark();
        public  void Page_Load(object sender, EventArgs e)
        {
            btnupdate.Visible = false; Panel2.Visible = false;
            if (!IsPostBack)
            {
                DataTable dt1 = cl.GetRecords(-99, "", -99, false, " and ");
                ddlclassid.DataSource = dt1;
                ddlclassid.DataTextField = "class_name";
                ddlclassid.DataValueField = "class_id";
                ddlclassid.DataBind();
                ddlclassid.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-Select-", "-99"));
                ddlstudentid.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-Select-", "-99"));

                //DataTable dt2 = stud.GetRecords(-99, "", -99, Convert.ToInt64(ddlclassid.SelectedValue.ToString()), "", "", "", "", "", "", -99, -99, "", -99, "", "", false, " and ");
                //ddlstudentid.DataSource = dt2;
                //ddlstudentid.DataTextField = "student_name";
                //ddlstudentid.DataValueField = "student_id";
                //ddlstudentid.DataBind();

                DataTable dt3 = sub.GetRecords(-99, "", -99, -99, false, " and ");
                ddlsubjectid.DataSource = dt3;
                ddlsubjectid.DataTextField = "subject_name";
                ddlsubjectid.DataValueField = "subject_id";
                ddlsubjectid.DataBind();

                DataTable dt4 = rem.GetRecords(-99, "", false, " and ");
                ddlremarkid.DataSource = dt4;
                ddlremarkid.DataTextField = "remark_name";
                ddlremarkid.DataValueField = "remark_id";
                ddlremarkid.DataBind();

                DataTable dt = rep.GetRecords(-99, "", -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, "", -99, false, " and ");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            btnupdate.Visible = true; btninsert.Visible = false;
            DataTable dt=rep.GetRecords(Convert.ToInt64(e.CommandArgument),"",-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,-99,"",-99,false," and ");
            tbreportid.Text = dt.Rows[0]["report_id"].ToString();
            ddexam.SelectedValue = dt.Rows[0]["examination"].ToString();
            ddlclassid.SelectedValue = dt.Rows[0]["class_id"].ToString();
            //HiddenField hid = (HiddenField)GridView1.FindControl("HiddenField1");
            ddlclassid_SelectedIndexChanged(null, null);
            ddlstudentid.SelectedValue = dt.Rows[0]["student_id"].ToString();
            tbrollno.Text=dt.Rows[0]["roll_no"].ToString();
            //ddlstudentid.SelectedValue = hid.Value;
            ddlsubjectid.SelectedValue = dt.Rows[0]["subject_id"].ToString();
            tbtheorymax.Text = dt.Rows[0]["max_theorymarks"].ToString();
            tbpracmax.Text = dt.Rows[0]["max_practicalmarks"].ToString();
            tbpromax.Text = dt.Rows[0]["max_projectmarks"].ToString();
            tbtheoryobt.Text = dt.Rows[0]["obt_theorymarks"].ToString();
            tbpracobt.Text = dt.Rows[0]["obt_practicalmarks"].ToString();
            tbproobt.Text = dt.Rows[0]["obt_projectmarks"].ToString();
            tbtotmarks.Text = dt.Rows[0]["total_maxmarks"].ToString();
            tbtobtmarks.Text = dt.Rows[0]["total_obtmarks"].ToString();
            tbper.Text = dt.Rows[0]["percentage"].ToString();
            tbgrade.Text = dt.Rows[0]["grade"].ToString();
            ddlremarkid.SelectedValue = dt.Rows[0]["remark_id"].ToString();

        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = rep.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, "", -99, false, " and ");
                rep.report_id = Convert.ToInt64(dt.Rows[0]["report_id"].ToString());
                rep.examination = dt.Rows[0]["examination"].ToString();
                rep.class_id = Convert.ToInt64(dt.Rows[0]["class_id"].ToString());
                rep.student_id = Convert.ToInt64(dt.Rows[0]["student_id"].ToString());
                rep.subject_id = Convert.ToInt64(dt.Rows[0]["subject_id"].ToString());
                rep.max_theorymarks = Convert.ToInt16(dt.Rows[0]["max_theorymarks"].ToString());
                rep.max_practicalmarks = Convert.ToInt16(dt.Rows[0]["max_practicalmarks"].ToString());
                rep.max_projectmarks = Convert.ToInt16(dt.Rows[0]["max_projectmarks"].ToString());
                rep.obt_theorymarks = Convert.ToInt16(dt.Rows[0]["obt_theorymarks"].ToString());
                rep.obt_practicalmarks = Convert.ToInt16(dt.Rows[0]["obt_practicalmarks"].ToString());
                rep.obt_projectmarks = Convert.ToInt16(dt.Rows[0]["obt_projectmarks"].ToString());
                rep.total_maxmarks = Convert.ToInt16(dt.Rows[0]["total_maxmarks"].ToString());
                rep.total_obtmarks = Convert.ToInt16(dt.Rows[0]["total_obtmarks"].ToString());
                rep.percentage = Convert.ToDecimal(dt.Rows[0]["percentage"].ToString());
                rep.grade = dt.Rows[0]["grade"].ToString();
                rep.remark_id = Convert.ToInt64(dt.Rows[0]["remark_id"].ToString());
                rep.isdeleted = true;
                rep.SaveRecords();
                Response.Redirect("~/admin/Report.aspx");
            }
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            rep.report_id = -99;
            rep.examination = ddexam.SelectedValue.ToString();
            rep.class_id = Convert.ToInt64(ddlclassid.SelectedValue.ToString());
            rep.student_id = Convert.ToInt64(ddlstudentid.SelectedValue.ToString());
            rep.subject_id = Convert.ToInt64(ddlsubjectid.SelectedValue.ToString());
            rep.max_theorymarks = Convert.ToInt16(tbtheorymax.Text);
            rep.max_practicalmarks = Convert.ToInt16(tbpracmax.Text);
            rep.max_projectmarks = Convert.ToInt16(tbpromax.Text);
            rep.obt_theorymarks = Convert.ToInt16(tbtheoryobt.Text);
            rep.obt_practicalmarks = Convert.ToInt16(tbpracobt.Text);
            rep.obt_projectmarks = Convert.ToInt16(tbproobt.Text);
            int a1 = Convert.ToInt16(tbtheorymax.Text);
            int a2 = Convert.ToInt16(tbpracmax.Text);
            int a3 = Convert.ToInt16(tbpromax.Text);
            int s1 = a1 + a2 + a3;
            tbtotmarks.Text = s1.ToString(); 
            int b1 = Convert.ToInt16(tbtheoryobt.Text);
            int b2 = Convert.ToInt16(tbpracobt.Text);
            int b3 = Convert.ToInt16(tbproobt.Text);
            int s2 = b1 + b2 + b3;
            tbtobtmarks.Text = s2.ToString(); 
            rep.total_maxmarks = Convert.ToInt16(tbtotmarks.Text);
            rep.total_obtmarks = Convert.ToInt16(tbtobtmarks.Text);
            decimal p1=Convert.ToDecimal(tbtobtmarks.Text);
            decimal p2 = Convert.ToDecimal(tbtotmarks.Text);
            decimal p =p1 / p2 * 100;
            
            if (Convert.ToInt64(p) < 40)
                tbgrade.Text = "Fail";
            if (Convert.ToInt64(p) < 50 && Convert.ToInt64(p) >= 40)
                tbgrade.Text = "C";
            if (Convert.ToInt64(p) < 60 && Convert.ToInt64(p) >= 50)
                tbgrade.Text = "B";
            if (Convert.ToInt64(p) <= 100 && Convert.ToInt64(p) >= 60)
                tbgrade.Text = "A";

            tbper.Text =  p.ToString();
            rep.percentage  =Math.Round(Convert.ToDecimal(tbper.Text));
            rep.grade = tbgrade.Text;
            rep.remark_id = Convert.ToInt64(ddlremarkid.SelectedValue.ToString());
            rep.isdeleted = false;
            rep.SaveRecords();
            Response.Redirect("~/admin/Report.aspx");
        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            rep.report_id = Convert.ToInt64(tbreportid.Text);
            rep.examination = ddexam.SelectedValue.ToString();
            rep.class_id = Convert.ToInt64(ddlclassid.SelectedValue.ToString());
            rep.student_id = Convert.ToInt64(ddlstudentid.SelectedValue.ToString());
            rep.subject_id = Convert.ToInt64(ddlsubjectid.SelectedValue.ToString());
            rep.max_theorymarks = Convert.ToInt16(tbtheorymax.Text);
            rep.max_practicalmarks = Convert.ToInt16(tbpracmax.Text);
            rep.max_projectmarks = Convert.ToInt16(tbpromax.Text);
            rep.obt_theorymarks = Convert.ToInt16(tbtheoryobt.Text);
            rep.obt_practicalmarks = Convert.ToInt16(tbpracobt.Text);
            rep.obt_projectmarks = Convert.ToInt16(tbproobt.Text);
            int a1 = Convert.ToInt16(tbtheorymax.Text);
            int a2 = Convert.ToInt16(tbpracmax.Text);
            int a3 = Convert.ToInt16(tbpromax.Text);
            int s1 = a1 + a2 + a3;
            tbtotmarks.Text = s1.ToString();
            int b1 = Convert.ToInt16(tbtheoryobt.Text);
            int b2 = Convert.ToInt16(tbpracobt.Text);
            int b3 = Convert.ToInt16(tbproobt.Text);
            int s2 = b1 + b2 + b3;
            tbtobtmarks.Text = s2.ToString();
            rep.total_maxmarks = Convert.ToInt16(tbtotmarks.Text);
            rep.total_obtmarks = Convert.ToInt16(tbtobtmarks.Text);
            decimal p1 = Convert.ToDecimal(tbtobtmarks.Text);
            decimal p2 = Convert.ToDecimal(tbtotmarks.Text);
            decimal p = p1 / p2 * 100;
            if (Convert.ToInt64(p) < 40)
                tbgrade.Text = "Fail";
            if (Convert.ToInt64(p) < 50 && Convert.ToInt64(p) >= 40)
                tbgrade.Text = "C";
            if (Convert.ToInt64(p) < 60 && Convert.ToInt64(p) >= 50)
                tbgrade.Text = "B";
            if (Convert.ToInt64(p) <= 100 && Convert.ToInt64(p) >= 60)
                tbgrade.Text = "A";
            tbper.Text = p.ToString();
            rep.percentage = Convert.ToDecimal(tbper.Text);
            rep.grade = tbgrade.Text;
            rep.remark_id = Convert.ToInt64(ddlremarkid.SelectedValue.ToString());
            rep.isdeleted = false;
            rep.SaveRecords();
            Response.Redirect("~/admin/Report.aspx");

        }

        protected void ddlclassid_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt2 = stud.GetRecords(-99, "", -99, Convert.ToInt64(ddlclassid.SelectedValue.ToString()), "", "", "", "", "", "", -99, -99, "", -99, "", "","","", false, " and ");
            ddlstudentid.DataSource = dt2;
            ddlstudentid.DataTextField = "student_name";
            ddlstudentid.DataValueField = "student_id";
            ddlstudentid.DataBind();
            
        }

        protected void ddlstudentid_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = stud.GetRecords(Convert.ToInt64(ddlstudentid.SelectedValue.ToString()), "", -99, -99, "", "", "", "", "", "", -99, -99, "", -99, "", "","","", false, " and ");
            tbrollno.Text = dt.Rows[0]["roll_no"].ToString();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        
    }
}
